package com.xydtech.ecard.statistics.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysUser;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.statistics.domain.EcardReportQuery;
import com.xydtech.ecard.statistics.service.IEcardReportQueryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 电子名片报告查询记录Controller
 *
 * @author xydtech
 * @date 2022-02-24
 */
@Controller
@RequestMapping("/statistics/queryReport")
public class EcardReportQueryController extends BaseController {
    private String prefix = "statistics/queryReport";

    @Autowired
    private IEcardReportQueryService ecardReportQueryService;

    @RequiresPermissions("statistics:queryReport:view")
    @GetMapping()
    public String queryReport() {
        return prefix + "/queryReport";
    }

    /**
     * 查询电子名片报告查询记录列表
     */
    @RequiresPermissions("statistics:queryReport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardReportQuery ecardReportQuery) {
        String salt = getSalt();//盐值
        startPage();
        List<EcardReportQuery> list = ecardReportQueryService.selectEcardReportQueryList(ecardReportQuery, salt);
        return getDataTable(list);
    }

    /**
     * 导出电子名片报告查询记录列表
     */
    @RequiresPermissions("statistics:queryReport:export")
    @Log(title = "电子名片报告查询记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardReportQuery ecardReportQuery) {
        String salt = getSalt();//盐值
        List<EcardReportQuery> list = ecardReportQueryService.selectEcardReportQueryList(ecardReportQuery, salt);
        ExcelUtil<EcardReportQuery> util = new ExcelUtil<EcardReportQuery>(EcardReportQuery.class);
        return util.exportExcel(list, "电子名片报告查询记录数据");
    }

    /**
     * 新增电子名片报告查询记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片报告查询记录
     */
    @RequiresPermissions("statistics:queryReport:add")
    @Log(title = "电子名片报告查询记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardReportQuery ecardReportQuery) {
        try {
            String salt = getSalt();//盐值
            SysUser user = ShiroUtils.getSysUser();
            ecardReportQuery.setDeptId(user.getDeptId());
            ecardReportQuery.setUserId(user.getUserId());
            ecardReportQuery.setQurtyTime(new Date());
            return toAjax(ecardReportQueryService.insertEcardReportQuery(ecardReportQuery, salt));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改电子名片报告查询记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        String salt = getSalt();//盐值
        EcardReportQuery ecardReportQuery = ecardReportQueryService.selectEcardReportQueryById(id, salt);
        mmap.put("ecardReportQuery", ecardReportQuery);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片报告查询记录
     */
    @RequiresPermissions("statistics:queryReport:edit")
    @Log(title = "电子名片报告查询记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardReportQuery ecardReportQuery) {
        String salt = getSalt();//盐值
        return toAjax(ecardReportQueryService.updateEcardReportQuery(ecardReportQuery, salt));
    }
}
